package cn.com.server.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import org.yaml.snakeyaml.Yaml;

public class YamlUtils {
	
	@SuppressWarnings("unchecked")
	public static LinkedHashMap<String,Object> getYamlSource(String name){
		InputStream in = null;
		try {
			Yaml yaml = new Yaml();
			in = MyResource.getInstance().getClass().getClassLoader().getResourceAsStream(name);
			LinkedHashMap<String,Object> result = (LinkedHashMap<String, Object>) yaml.load(in);
			in.close();
			return result;
		}catch(Exception e) {
			e.printStackTrace();
			if(in!=null) {
				try {
					in.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			return null;
		}
	}
	
//	public static void main(String[] args) {
////		Yaml yaml = new Yaml();
////        Iterable<Object> result = yaml.loadAll(MyResource.getInstance().getClass().getClassLoader().getResourceAsStream("application.yml"));
////        for (Object obj : result) {
////            System.out.println(obj.getClass());
////            System.out.println(obj);
////        }
////		System.out.println(JSON.toJSONString(YamlUtils.getYamlSource("application.yml")));
//		
////		LinkedHashMap<String,Object> map = new LinkedHashMap<>();
////		List<LinkedHashMap> as = new ArrayList<>();
////		for(int i=0;i<10;i++) {
////			LinkedHashMap<String,Object> maptmp = new LinkedHashMap<>();
////			maptmp.put("url", "/ppp"+i);
////			maptmp.put("value", map);
////			as.add(maptmp);
////		}
////		map.put("test", "*dsfasfasdasda");
////		map.put("ds", as);
////		LinkedHashMap<String,Object> map = new LinkedHashMap<>();
////		AjaxCode code = new AjaxCode();
////		code.setFail(new AjaxObject(-404,"请求失败"));
////		code.setSuccess(new AjaxObject(0,"请求成功"));
////		map.put("success", new AjaxObject(0,"请求成功"));
////		map.put("fail", new AjaxObject(-404,"请求失败"));
////		map.put("error", new AjaxObject(-1,"系统异常"));
////		Yaml y = new Yaml();
////		try {
////			y.dump(code, new FileWriter("D:\\aaaaa.yml"));
////		} catch (Exception e) {
////			e.printStackTrace();
////		}
//		//ajaxcode.yml
//		InputStream in = null;
//		try {
//			Yaml yaml = new Yaml();
//			in = MyResource.getInstance().getClass().getClassLoader().getResourceAsStream("ajaxcode.yml");
//			LinkedHashMap<String,AjaxObject> result = (LinkedHashMap<String, AjaxObject>) yaml.load(in);
//			System.out.println(JSON.toJSONString(result.get("success")));
//			in.close();
//		}catch(Exception e) {
//			e.printStackTrace();
//			if(in!=null) {
//				try {
//					in.close();
//				} catch (IOException e1) {
//					e1.printStackTrace();
//				}
//			}
//		}
//	}
}
